package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICartService;
import com.mmall.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * Created by hanchao on 2018/1/19.
 */
@RestController
@RequestMapping(value = "/cart/")
public class CartController {

    @Autowired
    private IProductService iProductService;
    @Autowired
    private ICartService iCartService;

    @RequestMapping(value = "list.do")
    public ServerResponse list(HttpSession session){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.list(user.getId());
    }

    @RequestMapping(value = "add.do")
    public ServerResponse add(HttpSession session, @RequestParam(value = "count") Integer count, @RequestParam(value = "productId") Integer productId){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.add(user.getId(),productId,count);
    }

    @RequestMapping(value = "update.do")
    public ServerResponse update(HttpSession session, @RequestParam(value = "count") Integer count, @RequestParam(value = "productId") Integer productId){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.update(user.getId(),productId,count);
    }

    @RequestMapping(value = "delet_product.do")
    public ServerResponse deleteProduct(HttpSession session,String productIds){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.deletProduct(user.getId(),productIds);
    }

    //全选
    @RequestMapping(value = "select_all.do")
    public ServerResponse selectAll(HttpSession session){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.selectOrUnSelect(user.getId(),Const.Cart.CHECKED,null);
    }
    // 全反选
    @RequestMapping(value = "unselect_all.do")
    public ServerResponse unSelectAll(HttpSession session){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.selectOrUnSelect(user.getId(),Const.Cart.UN_CHECKED,null);
    }
    // 单独选
    @RequestMapping(value = "select.do")
    public ServerResponse select(HttpSession session,Integer productId){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.selectOrUnSelect(user.getId(),Const.Cart.CHECKED,productId);
    }
    // 单独反选
    @RequestMapping(value = "unselect.do")
    public ServerResponse unSelect(HttpSession session,Integer productId){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.selectOrUnSelect(user.getId(),Const.Cart.UN_CHECKED,productId);
    }
    // 查询当前购物车产品数量 如果一个产品有十个，那么数量就是10
    @RequestMapping(value = "get_cart_product_count.do")
    public ServerResponse getCartProductCount(HttpSession session){
        User user =(User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createdByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登陆，请登录");
        }
        return iCartService.getCartProductCount(user.getId());
    }

}
